package com.sscl.baselibrary.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import com.sscl.baselibrary.exts.hideNavigationBar
import com.sscl.baselibrary.utils.BaseManager
import com.sscl.baselibrary.utils.PermissionUtil

@SuppressLint("CustomSplashScreen")
abstract class BasePermissionCheckActivity : Activity() {

    companion object {
        private const val PERMISSION_REQUEST_CODE = 1
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 静态常量
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    @Suppress("PropertyName")
    protected val TAG: String = javaClass.simpleName

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 属性声明
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    private var needCheckPermission = false

    /**
     * 权限请求回调
     */
    private val onPermissionRequestResult = object : PermissionUtil.OnPermissionRequestResult {
        /**
         * 权限请求成功
         */
        override fun permissionRequestSucceed() {
            permissionsRequested()
        }

        /**
         * 权限请求失败
         *
         * @param failedPermissions 请求失败的权限
         */
        override fun permissionRequestFailed(failedPermissions: Array<String>) {
            val anyPermissionAlwaysDenied = PermissionUtil.isAnyPermissionAlwaysDenied(
                this@BasePermissionCheckActivity,
                *failedPermissions
            )

            if (anyPermissionAlwaysDenied) {
                val needToSetting = permissionsAlwaysDenied(failedPermissions)
                if (needToSetting) {
                    toSettingActivity()
                } else {
                    permissionRequestFailed()
                }
            } else {
                val needRequestAgain = permissionsDenied(failedPermissions)
                if (needRequestAgain) {
                    checkPermission(permissionsNeedRequest())
                } else {
                    permissionRequestFailed()
                }
            }
        }

    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 抽象方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    abstract fun needHideNavigationBar(): Boolean

    /**
     * 需要请求的权限
     */
    abstract fun permissionsNeedRequest(): Array<String>

    /**
     * 权限请求成功
     */
    abstract fun permissionsRequested()

    /**
     * 有权限被永久拒绝
     * @param failedPermissions 被拒绝的权限
     * @return 是否跳转到系统权限设置界面
     */
    abstract fun permissionsAlwaysDenied(failedPermissions: Array<String>): Boolean

    /**
     * 权限被用户拒绝
     * @param failedPermissions 被拒绝的权限
     * @return 是否继续进行权限请求
     */
    abstract fun permissionsDenied(failedPermissions: Array<String>): Boolean

    /**
     * 权限请求失败
     */
    abstract fun permissionRequestFailed()

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 子类可用方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    /**
     * 跳转到设置界面
     */
    protected fun toSettingActivity() {
        PermissionUtil.toSettingActivity(this@BasePermissionCheckActivity)
        BaseManager.handler.postDelayed({
            needCheckPermission = true
        }, 500)
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 重写方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (needHideNavigationBar()) {
            hideNavigationBar()
        }
        checkPermission(permissionsNeedRequest())
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionUtil.onRequestPermissionsResult(this, requestCode, permissions, grantResults)
    }

    override fun onResume() {
        super.onResume()
        if (needCheckPermission) {
            needCheckPermission = false
            checkPermission(permissionsNeedRequest())
        }
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 私有方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    /**
     * 请求权限
     */
    private fun checkPermission(permissions: Array<String>) {
        val hasPermissions = PermissionUtil.hasPermissions(this, *permissions)
        if (hasPermissions) {
            permissionsRequested()
        } else {
            PermissionUtil.setOnPermissionRequestResult(onPermissionRequestResult)
            PermissionUtil.requestPermission(this, PERMISSION_REQUEST_CODE, *permissions)
        }
    }
}